package JdbcUitils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class GetDistanceByGPSArrs {
public static void main(String[] args) {
	//getDistanceByGPSArrsSer();
}
    public static String getDistanceByGPSArrsSer(String startGps,String endGpsS){
    	String result = readContentFromGet("http://api.map.baidu.com/routematrix/v2/riding?output=json&origins="
				+startGps
				+"&destinations="
				+endGpsS
				+"&ak=HdveE6DXEvvXf9Ppl5Xjwvm73C1hecfX");
    	System.out.println("http://api.map.baidu.com/routematrix/v2/riding?output=json&origins="
				+startGps
				+"&destinations="
				+endGpsS
				+"&ak=HdveE6DXEvvXf9Ppl5Xjwvm73C1hecfX");
    	
    	return result;
	}
    public static String readContentFromGet(String url){
    	String lines="";
	    try {
	    	 URL getUrl = new URL(url);
	         HttpURLConnection connection = (HttpURLConnection) getUrl
	                 .openConnection();
	         connection.connect();
	         BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
	         StringBuffer sb = new StringBuffer();
	 		int ch;
	 		while ((ch = reader.read()) > -1)
	 			sb.append((char) ch);
	 		lines = sb.toString();
	         reader.close();
	         connection.disconnect();
		} catch (Exception e) {
			// TODO: handle exception
		}
       
        return lines;
 }
	public static String connectURL(String dest_url, String commString) {
		String rec_string = "";
		URL url = null;
		HttpURLConnection urlconn = null;
		OutputStream out = null;
		BufferedReader rd = null;
		try {
			url = new URL(dest_url);
			urlconn = (HttpURLConnection) url.openConnection();
			urlconn.setReadTimeout(1000 * 30);
			urlconn.setRequestMethod("POST");
			urlconn.setDoInput(true);
			urlconn.setDoOutput(true);
			out = urlconn.getOutputStream();
			out.write(commString.getBytes("UTF-8"));
			out.flush();
			out.close();
			rd = new BufferedReader(new InputStreamReader(urlconn.getInputStream()));
			StringBuffer sb = new StringBuffer();
			int ch;
			while ((ch = rd.read()) > -1)
				sb.append((char) ch);
			rec_string = sb.toString();
		} catch (Exception e) {
			//Log.error(e, e);
			return "";
		} finally {
			try {
				if (out != null) {
					out.close();
				}
				if (urlconn != null) {
					urlconn.disconnect();
				}
				if (rd != null) {
					rd.close();
				}
			} catch (Exception e) {
				//Log.error(e, e);
			}
		}
		return rec_string;
	}
}
